package kz.asoft;

import java.awt.Color;

import java.util.Calendar;

import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import kz.asoft.am.common.RisksAppModule;
import kz.asoft.utils.ADFUtils;
import kz.asoft.utils.Utils;

import oracle.adf.view.faces.bi.component.pivotTable.CellFormat;
import oracle.adf.view.faces.bi.component.pivotTable.HeaderCellContext;
import oracle.adf.view.rich.component.rich.input.RichInputText;
import oracle.adf.view.rich.component.rich.input.RichSelectOneChoice;
import oracle.adf.view.rich.component.rich.nav.RichCommandButton;
import oracle.adf.view.rich.render.ClientEvent;

import oracle.bali.ewt.graphics.ColorUtils;

import oracle.dss.util.QDR;

import oracle.jbo.Row;
import oracle.jbo.domain.Number;
import oracle.jbo.uicli.binding.JUCtrlValueBinding;


public class PlntdCtrlManageBean {
    private RichInputText searchText;
    private RichSelectOneChoice year;

    public PlntdCtrlManageBean() {
    }

    /**
     * Поиск по наименованию объекта контроля
     * @param actionEvent
     */
    public void searchCtrlObj(ActionEvent actionEvent) {
        RisksAppModule appModule =
            (RisksAppModule)ADFUtils.getApplicationModuleForDataControl("RisksAppModuleDataControl");
        appModule.searchCtrlObj(searchText.getValue() == null ? "" :
                                searchText.getValue().toString());
    }

    /**
     * Анализ полноты охвата ГО тематическими контрольными мероприятиями СК за 2 года
     * @param actionEvent
     */
    public void getPlntdCtrlObjReport(ActionEvent actionEvent) {
        JUCtrlValueBinding ctrlObjView =
            ADFUtils.findCtrlBinding("SCtrlObjsView1");
        Row row = ctrlObjView.getIteratorBinding().getCurrentRow();
        RisksAppModule appModule =
            (RisksAppModule)ADFUtils.getApplicationModuleForDataControl("RisksAppModuleDataControl");
        int yearInt;
        if (year != null && year.getValue() != null) {
            yearInt = Integer.valueOf(year.getValue().toString());
        } else {
            yearInt = Calendar.getInstance().get(Calendar.YEAR);
        }
        appModule.getPlntdCtrlObjReport((Number)row.getAttribute("Id"),
                                        yearInt);
    }
    
    /**
     * Анализ полноты охвата по всем ГО тематическими контрольными мероприятиями за 2 года
     * @param actionEvent
     */
    public void getPlntdAllCtrlObjReport(ActionEvent actionEvent) {
        RisksAppModule appModule =
            (RisksAppModule)ADFUtils.getApplicationModuleForDataControl("RisksAppModuleDataControl");
        int yearInt;
        if (year != null && year.getValue() != null) {
            yearInt = Integer.valueOf(year.getValue().toString());
        } else {
            yearInt = Calendar.getInstance().get(Calendar.YEAR);
        }
        appModule.getPlntdAllCtrlObjReport(yearInt);
    }

    /**
     * Custom header format
     * @param headerCellContext
     * @return
     */
    public CellFormat getHeaderFormat(HeaderCellContext headerCellContext) {
        CellFormat cellFormat = new CellFormat(null, null, null);
        String color = null;
        if (headerCellContext.getValue() != null) {
            QDR qdr = headerCellContext.getQDR();
            Object sign = qdr.getDimMember("Sign");
            if (sign != null) {
                if (sign.toString().equals("0")) {
                    color =
                            "background-color:" + ColorUtils.colorToHTML(Color.RED) +
                            ";";
                } else {
                    color =
                            "background-color:" + ColorUtils.colorToHTML(Color.YELLOW) +
                            ";";
                }
                cellFormat.setStyle(color);
                cellFormat.setTextStyle("display: none;");
            }
            if(headerCellContext.getValue().equals("TOTAL")){
                cellFormat.setNewRawValue(Utils.getLocalizedText("TOTAL"));    
            }
        }
        return cellFormat;
    }
    
    /**
     * Call action by dbclick
     * @param clientEvent
     */
    public void dbClickEvent(ClientEvent clientEvent) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        UIViewRoot root = facesContext.getViewRoot();
        RichCommandButton button = (RichCommandButton)root.findComponent("r1:pc1:cb_db_click1");
        if (button != null) {
            ActionEvent actionEvent = new ActionEvent(button);
            actionEvent.queue();
        }
    }

    public void setSearchText(RichInputText searchText) {
        this.searchText = searchText;
    }

    public RichInputText getSearchText() {
        return searchText;
    }

    public void setYear(RichSelectOneChoice year) {
        this.year = year;
        int yearInt = Calendar.getInstance().get(Calendar.YEAR);
        year.setValue(yearInt);
    }

    public RichSelectOneChoice getYear() {
        return year;
    }
}
